package com.qcq.wecheck.controller;

import cn.afterturn.easypoi.entity.vo.MapExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.qcq.wecheck.auth.Auth;
import com.qcq.wecheck.result.CommonResult;
import com.qcq.wecheck.service.TestService;
import com.qcq.wecheck.util.AesUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 测试控制器
 * </p>
 *
 * @author O
 * @version 1.0
 * @since 2018/9/12
 */
@RestController
@RequestMapping("/test")
public class TestController {
	@Resource
	private TestService testService;

	@GetMapping
	public CommonResult test(Integer code, Boolean success, String message, String content) throws Exception {
		System.out.println("接收到消息...");

		System.out.println(content);
		System.out.println(code);
		System.out.println(success);
		System.out.println(message);

		if (content != null) {
			content = AesUtils.aesDecrypt(content, "abcderj2i2i281jl");
			System.out.println("decrpyt ... content: \n" + content);
		}

		return CommonResult.success(testService.getOne());
	}

	@GetMapping("/failure")
	@Auth
	public CommonResult failure() {
		return CommonResult.failure();
	}

	@RequestMapping("/export")
	public void downloadByPoiBaseView(ModelMap modelMap, HttpServletRequest request,
	                                  HttpServletResponse response) {
		List<ExcelExportEntity> entities = new ArrayList<ExcelExportEntity>();
		ExcelExportEntity exportEntity;
		exportEntity = new ExcelExportEntity("2017-01-01", "2017-01-01");
		List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
		temp.add(new ExcelExportEntity("完成情况", "status"));
		temp.add(new ExcelExportEntity("1", "1"));
		temp.add(new ExcelExportEntity("2", "2"));
		temp.add(new ExcelExportEntity("3", "3"));
		exportEntity.setList(temp);
		entities.add(exportEntity);

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		for (int i = 0; i < 10; i++) {
			map = new HashMap<String, Object>();
			map.put("status", "√");
			map.put("1", "time1");
			map.put("2", "time2");
			map.put("3", "time3");
			List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
			tempList.add(map);
			map.put("2017-01-01", tempList);

			list.add(map);
		}

		ExportParams params = new ExportParams("title...", "测试", ExcelType.XSSF);
		params.setFreezeCol(2);
		modelMap.put(MapExcelConstants.MAP_LIST, list);
		modelMap.put(MapExcelConstants.ENTITY_LIST, entities);
		modelMap.put(MapExcelConstants.PARAMS, params);
		modelMap.put(MapExcelConstants.FILE_NAME, "EasypoiMapExcelViewTest");
		PoiBaseView.render(modelMap, request, response, MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW);

	}
}
